const eventService = require('../service/event.service')
const {
	EVENT_CTEATE_FAILED,
	EVENT_REMOVE_FAILED,
	EVENT_UPDATE_FAILED,
	EVENT_SEARCH_FAILED,
	UPDATE_EVENT_STATUS_FAILED,
	EVENT_REGISTER_FAILED,
	EVENT_CANCEL_FAILED
} = require('../config/error')
class EventController {
	async create(ctx, next) {
		try {
			const insertInfo = ctx.request.body
			insertInfo.start_date = new Date(insertInfo.start_date)
			insertInfo.end_date = new Date(insertInfo.end_date)
			console.log(insertInfo)
			const res = await eventService.create(insertInfo)
			ctx.body = {
				code: 0,
				message: '发布活动成功'
			}
		} catch (err) {
			console.log(err)
			ctx.app.emit('error', EVENT_CTEATE_FAILED, ctx)
		}
	}
	async remove(ctx, next) {
		try {
			const { event_id } = ctx.params
			const res = await eventService.remove(event_id)
			ctx.body = {
				code: 0,
				message: '删除活动成功'
			}
		} catch (err) {
			ctx.app.emit('error', EVENT_REMOVE_FAILED, ctx)
		}
	}
	async update(ctx, next) {
		try {
			const { event_id } = ctx.params
			const insertInfo = ctx.request.body
			insertInfo.start_date = new Date(insertInfo.start_date)
			insertInfo.end_date = new Date(insertInfo.end_date)
			const res = await eventService.update(event_id, insertInfo)
			ctx.body = {
				code: 0,
				message: '更新活动成功'
			}
		} catch (err) {
			console.log(err)
			ctx.app.emit('error', EVENT_UPDATE_FAILED, ctx)
		}
	}
	async search(ctx, next) {
		try {
			const queryInfo = ctx.query
			const res = await eventService.search(queryInfo)
			ctx.body = {
				code: 0,
				message: '查询成功',
				data: res
			}
		} catch (error) {
			ctx.app.emit('error', EVENT_SEARCH_FAILED, ctx)
		}
	}
	// 修改活动状态
	async updateStatus(ctx, next) {
		try {
			const { event_id } = ctx.params
			const { status } = ctx.query
			const res = await eventService.updateStatus(event_id, status)
			ctx.body = {
				code: 0,
				message: '修改活动状态成功'
			}
		} catch (err) {
			ctx.app.emit('error', UPDATE_EVENT_STATUS_FAILED, ctx)
		}
	}
	// 通过id查询活动
	async searchEventById(ctx, next) {
		try {
			const { event_id } = ctx.params
			const res = await eventService.searchEventById(event_id)
			ctx.body = {
				code: 0,
				message: '查询活动成功',
				data: res
			}
		} catch (err) {
			ctx.app.emit('error', EVENT_SEARCH_FAILED, ctx)
		}
	}

	// 通过id查询参与的人员
	async searchEventMember(ctx, next) {
		try {
			const { event_id } = ctx.params
			const res = await eventService.searchEventMember(event_id)
			ctx.body = {
				code: 0,
				message: '查询成功',
				data: res
			}
		} catch (error) {
			ctx.app.emit('error', EVENT_SEARCH_FAILED, ctx)
		}
	}

	// 报名活动
	async joinEevent(ctx, next) {
		try {
			const { event_id, user_id } = ctx.params
			const res = await eventService.joinEvent(event_id, user_id)
			ctx.body = {
				code: 0,
				message: '报名成功'
			}
		} catch (error) {
			ctx.app.emit('error', EVENT_REGISTER_FAILED, ctx)
		}
	}
	// 退出活动
	async quitEvent(ctx, next) {
		try {
			const { event_id, user_id } = ctx.params
			const res = await eventService.quitEvent(event_id, user_id)
			ctx.body = {
				code: 0,
				message: '取消报名成功'
			}
		} catch (error) {
			ctx.app.emit('error', EVENT_CANCEL_FAILED, ctx)
		}
	}
	// 根据社团id查询活动
	async searchEventByClubId(ctx, next) {
		try {
			const { club_id } = ctx.params
			const res = await eventService.searchEventByClubId(club_id)
			ctx.body = {
				code: 0,
				message: '查询成功',
				data: res
			}
		} catch (error) {

		}
	}

	// 查询所有社团发布的活动个数
	async searchPublishEventCount(ctx, next) {
		try {
			const res = await eventService.searchPublishEventCount()
			ctx.body = {
				code: 0,
				message: '查询成功',
				data: res
			}
		} catch (error) {

		}
	}
}

module.exports = new EventController()
